﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using QLKS.Templates;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmKhaiBaoTienTe.xaml
    /// </summary>
    public partial class frmKhaiBaoTienTe : FormTrans
    {
        public bool UpdateSucces = false;
        DataTable Data = new DataTable();

        Command cm;

        public frmKhaiBaoTienTe()
        {
            InitializeComponent();
            IsEditMode = true;
            cm = Command.New;
        }

        public frmKhaiBaoTienTe(DataTable tb, Command _cm)
        {
            InitializeComponent();
            IsEditMode = _cm == Command.View ? false : true;
            cm = _cm;

            if (tb == null)
                Data = ViewModels.TienTeViewModel.CreateNewTienTe();
            else
                Data = tb.Copy();
            if (cm == Command.Copy)
                Data.Rows[0]["matt"] = string.Empty;
            gridMain.DataContext = Data.DefaultView;
        }

        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmKhaiBaoTienTe), new UIPropertyMetadata(true));

        public double TyGia
        {
            get { return (double)GetValue(TyGiaProperty); }
            set { SetValue(TyGiaProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TyGia.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TyGiaProperty =
            DependencyProperty.Register("TyGia", typeof(double), typeof(frmKhaiBaoTienTe), new UIPropertyMetadata(null));

        //Kiểm tra dữ liệu hợp lệ
        private bool KiemTra()
        {
            if (string.IsNullOrEmpty(txtMaTienTe.Text))
            {
                MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập mã":"input id, please", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtMaTienTe.Focus();
                return false;
            }
            else
                if (txtTyGia.Value == null || (double)txtTyGia.Value == 0)
                {
                    MessageBox.Show(M_LAN.Equals("V")?"Chưa vào tỷ giá":"input exchange rate,please", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtTyGia.Focus();
                    return false;
                }
            return true;
        }

        //Save
        void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu
            if (KiemTra())
            {
                if (ViewModels.TienTeViewModel.ThemTienTe(Data))
                {
                    this.UpdateSucces = true;
                    MessageBox.Show(M_LAN.Equals("V")?"Thực hiện thành công!":"Perform successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    Close();
                }
            }
        }

        void tbMain_Cm_Exit()
        {
            this.Close();
        }

        private void cbbMaTienTe_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string ma = (sender as ComboBox).SelectedValue.ToString();
            TyGia = double.Parse(ViewModels.TienTeViewModel.GetDmTienTe().Select("ma like '" + ma + "'")[0]["tygia"].ToString());
        }

        private void txtTyGia_GotFocus(object sender, RoutedEventArgs e)
        {
            txtTyGia.SelectAll();
        }

        private void FrmKhaiBaoTienTe_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable tb = ViewModels.TienTeViewModel.GetDmTienTe();
            if (IsEditMode)
                tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);

            txtMaTienTe.IsReadOnly = !(cm == Command.New || cm == Command.Copy);
            txtMoTa.Focus();
            if (!(bool)txtMaTienTe.IsReadOnly)
                txtMaTienTe.Focus();
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
        }
    }
}
